<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>文字转语音</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #f0f2f5;
        }

        .container {
            background-color: #fff;
            border-radius: 16px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            width: 80%;
            max-width: 800px; /* 增加宽度以适应侧边栏 */
            overflow: hidden;
            display: flex;
            flex-direction: row;
            align-items: stretch;
            padding: 30px;
        }

        .sidebar {
            width: 20%;
            padding: 30px;
            background-color: #f9f9f9;
            border-right: 1px solid #ddd;
        }

        .main-content {
            width: 80%;
            padding: 30px;
        }

        h1 {
            margin-bottom: 20px;
            color: #333;
            font-size: 24px;
        }

        .content {
            display: flex;
            flex-direction: column;
            align-items: center;
            width: 100%;
        }

        #ttsTextarea {
            width: 100%;
            margin-bottom: 20px;
            padding: 15px;
            border: 1px solid #e0e0e0;
            border-radius: 8px;
            resize: vertical;
            font-size: 16px;
            outline: none;
            transition: border-color 0.3s;
        }

        #ttsTextarea:focus {
            border-color: #007bff;
        }

        #submitBtn {
            padding: 12px 24px;
            background-color: #007bff;
            color: #fff;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s;
        }

        #submitBtn:hover {
            background-color: #0056b3;
        }

        #submitBtn:disabled {
            background-color: #ccc;
            cursor: not-allowed;
        }

        #ttsResponse {
            margin-top: 20px;
            text-align: center;
            width: 100%;
        }

        .loading, .completed, .error {
            margin-top: 10px;
            font-size: 16px;
        }

        .loading {
            color: #007bff;
        }

        .completed {
            color: green;
        }

        .error {
            color: red;
        }

        audio {
            max-width: 100%;
            border: 1px solid #ddd;
            border-radius: 8px;
            margin-top: 20px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }

        .sidebar h2 {
            margin-bottom: 20px;
            color: #333;
            font-size: 20px;
        }

        .sidebar select {
            width: 100%;
            padding: 10px;
            border: 1px solid #e0e0e0;
            border-radius: 8px;
            font-size: 16px;
            outline: none;
            transition: border-color 0.3s;
        }

        .sidebar select:focus {
            border-color: #007bff;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="sidebar">
        <h2>选择语音</h2>
        <select id="voiceSelect">
            <!-- 选项将通过 JavaScript 动态加载 -->
        </select>
    </div>
    <div class="main-content">
        <h1>文字转语音</h1>
        <div class="content">
            <div id="ttsResponse"></div>
            <form id="ttsForm">
                <textarea name="input" id="ttsTextarea" cols="30" rows="5" placeholder="请输入文字" required></textarea>
                <button type="submit" id="submitBtn">生成音频</button>
            </form>
        </div>
    </div>
</div>

<script src="./jquery.min.js"></script>
<script>
    $(document).ready(function() {
        // 动态加载语音选项
        $.get("/api/voices", function(data) {
            var select = $("#voiceSelect");
            data.forEach(function(voice) {
                select.append($('<option>', {
                    value: voice,
                    text: voice
                }));
            });
        });

        // 监听表单提交事件
        $("#ttsForm").submit(function(event) {
            event.preventDefault();
            if ($("#ttsTextarea").val().length < 5) {
                alert("文本太短了");
            } else {
                $("#submitBtn").prop('disabled', true);
                $("#ttsResponse").html('<div class="loading">音频生成中，请稍候...</div>');
                var voice = $("#voiceSelect").val(); // 获取选择的语音
                $.post("/ai/tts", { input: $("#ttsTextarea").val(), voice: voice }, function(resp) {
                    $("#ttsResponse").html(`
                        <audio controls>
                            <source src="${resp}" type="audio/mpeg">
                        </audio>
                    `);
                }).fail(function() {
                    $("#ttsResponse").html('<div class="error">生成失败，请重试</div>');
                }).always(function() {
                    $("#submitBtn").prop('disabled', false);
                });
            }
        });

        // 监听 Enter 键按下事件
        $("#ttsTextarea").keydown(function(event) {
            if (event.key === "Enter" && !event.shiftKey) {
                event.preventDefault();
                $("#ttsForm").submit();
            }
        });
    });
</script>
</body>
</html>
